import os

import pygame

from shared_variables import BLACK


class ColleagueList:
    def __init__(self, screen, position, colleagues):
        self.screen = screen
        self.position = position
        self.colleagues = colleagues
        self.font = pygame.font.SysFont("方正粗黑宋简体", 24)  # 设置列表字体
        self.text_data = []  # 存储全部文本数据
        self.selected_colleague = None

        # 预先生成文本数据
        self.generate_text_data()

    def update(self, mouse_pos):
        # 更新选中的同事
        self.selected_colleague = None

        for text_data in self.text_data:
            if text_data['text_rect'].collidepoint(mouse_pos):
                self.selected_colleague = text_data['colleague']
                break

    def generate_text_data(self):
        # 生成文本数据
        x, y = self.position
        for _, colleague in self.colleagues.iterrows():
            # 绘制头像
            image_path = os.path.join(os.curdir, 'assets/head', f'{colleague["name"]}.png')
            avatar_path = pygame.image.load(image_path)
            avatar = pygame.transform.scale(avatar_path, (36, 36))
            avatar_rect = avatar.get_rect(topleft=(x, y))

            # 绘制姓名文本
            text = self.font.render(colleague['name'], True, (0, 0, 0))
            text_rect = text.get_rect(topleft=(avatar_rect.right + 10, y))

            # 绘制关系文本
            relationship = colleague['relationship']
            color_map = {
                "死对头": (255, 0, 0),
                "相互不喜欢": (255, 153, 153),
                "不熟": (255, 255, 0),
                "喜欢": (153, 255, 153),
                "密友": (0, 128, 0)
            }
            relationship_color = color_map.get(relationship, BLACK)
            relationship_text = self.font.render(relationship, True, relationship_color)
            relationship_rect = relationship_text.get_rect(topleft=(text_rect.right + 10, y))

            self.text_data.append({
                'avatar': avatar,
                'avatar_rect': avatar_rect,
                'text': text,
                'text_rect': text_rect,
                'relationship_text': relationship_text,
                'relationship_rect': relationship_rect,
                'colleague': colleague
            })
            y += max(avatar_rect.height, text_rect.height, relationship_rect.height) + 10  # 设置下一个项目的纵坐标位置

    def draw(self):
        # 绘制同事列表
        for text_data in self.text_data:
            # 计算蒙层的大小和位置
            overlay_rect = text_data['avatar_rect'].union(text_data['text_rect']).union(
                text_data['relationship_rect']).inflate(20, 10)
            overlay = pygame.Surface((overlay_rect.width, overlay_rect.height), pygame.SRCALPHA)
            overlay.fill((255, 255, 255, 128))  # 白色半透明

            # 绘制蒙层到屏幕
            self.screen.blit(overlay, overlay_rect.topleft)

            # 绘制头像、姓名文本和关系文本
            self.screen.blit(text_data['avatar'], text_data['avatar_rect'].topleft)
            self.screen.blit(text_data['text'], text_data['text_rect'].topleft)
            self.screen.blit(text_data['relationship_text'], text_data['relationship_rect'].topleft)
    def get_selected_colleague(self):
        # 返回选中的同事
        return self.selected_colleague
